package com.canbot.u05.activity.presidemode.newcode;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.customvoice.tool.GridSpacingItemDecoration;
import com.canbot.u05.activity.presidemode.newcode.adapter.NChoiceMusicAdapter;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.NetLoadUtil;
import com.canbot.u05.activity.presidemode.newcode.bean.VoiceBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IItemClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IResponse;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NChoiceMusicActivity extends BaseHostActivity {
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private VoiceBean selectVoiceBean;
    private NChoiceMusicAdapter nChoiceMusicAdapter;
    private List<VoiceBean> arrayList;
    private static final String TAG = "NChoiceMusicActivity";

    @Override
    protected void initEventData() {
        initIntent();
        initCommonHead();
        recycleView.setLayoutManager(new GridLayoutManager(this, 4));
        arrayList = new ArrayList<>();
        ((SimpleItemAnimator) recycleView.getItemAnimator()).setSupportsChangeAnimations(false);
        recycleView.addItemDecoration(new GridSpacingItemDecoration(4, (int) getResources().getDimension(R.dimen.x6), false));
        nChoiceMusicAdapter = new NChoiceMusicAdapter(arrayList, this, new IItemClick() {
            @Override
            public void onClick(int position, View v) {
                VoiceBean danceBean = arrayList.get(position);
                if (selectVoiceBean != null && !danceBean.equals(selectVoiceBean)) {
                    int i = arrayList.indexOf(selectVoiceBean);
                    if (i >= 0) {
                        arrayList.get(i).setSelect(false);
                    }
                }
                selectVoiceBean = danceBean;
                selectVoiceBean.setSelect(!selectVoiceBean.isSelect());
                nChoiceMusicAdapter.notifyDataSetChanged();
                if (selectVoiceBean.isSelect()) {
                    MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, selectVoiceBean.path);
                } else {
                    MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stopSound");
                }
            }
        });
        recycleView.setAdapter(nChoiceMusicAdapter);
        initData();
    }

    private Dialog dialogLoading;

    private void initData() {
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        NetLoadUtil.loadAudio(this, new IResponse<List<Audio>>() {
            @Override
            public void onSucusse(List<Audio> result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                if (result != null) {
                    for (Audio audio : result) {
                        VoiceBean voiceBean = new VoiceBean();
                        voiceBean.languageType = NModuleType.VOICE_MUSIC;
                        voiceBean.path = audio.getPath();
                        voiceBean.content = audio.getName();
                        voiceBean.time = (int) audio.getTime();
                        if (voiceBean.equals(selectVoiceBean)) {
                            voiceBean.setSelect(true);
                        }
                        arrayList.add(voiceBean);
                    }
                    runOnUiThread(() -> nChoiceMusicAdapter.notifyDataSetChanged());
                }
            }

            @Override
            public void onFailuer(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                ToastTools.newInstance(getApplicationContext()).showText(result);
            }
        });
    }

    private void initCommonHead() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    if (selectVoiceBean != null && selectVoiceBean.isSelect()) {
                        Intent intent = new Intent();
                        intent.putExtra(DATA, selectVoiceBean);
                        setResult(RESULT_OK, intent);
                        finish();
                    } else {
                        Toast.makeText(this, getString(R.string.music_null_tip), Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        });
    }

    private void initIntent() {
        selectVoiceBean = (VoiceBean) getIntent().getSerializableExtra(DATA);
        int requestCode = getIntent().getIntExtra(KEY_REQUESTCODE, -1);
        if (requestCode == NModuleType.DANCE_MUSIC) {
            MapVoiceAndEyes.VoiceAndEyesInfo voiceTip = new MapVoiceAndEyes.VoiceAndEyesInfo(MapVoiceAndEyes.PRESENTER_VOICE_DIR + "jbkz_wdxz_u05_001.wav", "26");
            MapVoiceAndEyes.playMapVoiceAndEyes(voiceTip);
        }
    }

    private List<VoiceBean> mockData() {
        ArrayList<VoiceBean> strings = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            VoiceBean voiceBean = new VoiceBean();
            voiceBean.content = "item" + i;
            voiceBean.time = i + 1;
            voiceBean.languageType = NModuleType.VOICE_MUSIC;
            strings.add(voiceBean);
        }
        return strings;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nchoice_music;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoadingDialogUtils.closeDialog(dialogLoading);
        MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stopSound");
    }
}
